package com.lazily.try_;


/**
 * @Author: GZQ
 * @date: 2025/2/23 14:26
 * @description: try-catch处理异常的注意细节2
 */

public class TryCatchDetail02 {
	// 程序入口
	public static void main(String[] args) {
		/*
			1. 如果try代码块可能有多个异常
			2. 可以使用多个catch 分别捕获不同的异常, 相应处理
			3. 要求子类异常要写在前面, 父类异常写在后面
		 */
		try {
			Person person = new Person();
			person = null;
			System.out.println(person.getName()); // NullPointerException
			int n1 = 10;
			int n2 = 0;
			int res = n1 / n2; // ArithmeticException
		} catch (NullPointerException e) {
			System.out.println("空指针异常=" + e.getMessage());
		} catch (ArithmeticException e) {
			System.out.println("数学异常=" + e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
		}
	}
}


class Person {
	private String name = "John";

	public String getName() {
		return name;
	}
}